package com.telaxo.shooter2012.foes {
	import flash.filters.GlowFilter;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import com.telaxo.shooter2012.gfx.Ship;
	import com.telaxo.shooter2012.interfaces.Entity;
	import com.telaxo.shooter2012.interfaces.Vaisseau;
	import com.telaxo.shooter2012.projectiles.BalleEnnemi;

	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 * @author Benjamin
	 */
	public class Stinger extends MovieClip implements Entity, Vaisseau {
		[Embed(source="../gfx/swf/Stinger.swf")]
		private var Ressource : Class;
		private var stageRef : Stage;
		private var vy : Number = 2;
		private var ay : Number = .01;
		private var target : Ship;
		private var image : MovieClip;
		private var hitBox : Sprite;
		private var _state : Number;
		public static var VIVANT : Number = 1;
		public static var DETRUIT : Number = 2;
		public static var DESTRUCTION_COMPLETE : Number = 3;
		private var myVitX : Number;
		private var myVitY : Number;
		private var myTargetXvit : Number;
		private var myTargetYvit : Number;
		private var myXlimit : uint;
		private var myYlimit : uint;
		private var myDirectionTimer : Timer;
		private var myInitTimer : Timer;

		public function Stinger(s : Stage, t : Ship) {
			stageRef = s;
			target = t;

			var image : MovieClip = new Ressource();
			addChild(image);

			myVitX = 0;
			myVitY = 0;
			myDirectionTimer = new Timer(1000);
			myDirectionTimer.addEventListener(TimerEvent.TIMER, onDirectionTimer, false, 0, true);
			myInitTimer = new Timer(10);
			myInitTimer.addEventListener(TimerEvent.TIMER, fadeIn, false, 0, true);
			filters = [new GlowFilter(0xff0000)];
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);

			cacheAsBitmap = true;

			/*	 else if(Math.floor(rand) == 1){
			x = Math.random()*(stage.stageWidth);
			y=0;	
			addEventListener(Event.ENTER_FRAME, loop_lv5, false, 0, true);
			 */
			// }
		}

		private function init(e : Event) : void {
			removeEventListener(Event.ENTER_FRAME, init);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);

			myXlimit = stage.stageWidth;
			myYlimit = stage.stageHeight;

			alpha = 0;

			x = Math.random() * myXlimit;
			y = Math.random() * myYlimit;

			if (x == -1)
				x = Math.random() * myXlimit;
			if (y == -1)
				y = Math.random() * myYlimit;

			myVitX = 0;
			myVitY = 0;

			myTargetXvit = Math.random() * 4 - 2;
			myTargetYvit = Math.random() * 4 - 2;

			myDirectionTimer.start();
			myInitTimer.start();
		}

		private function fadeIn(e : TimerEvent) : void {
			alpha += 0.01;
			scaleX = alpha;
			scaleY = alpha;
			if (alpha >= 1) {
				myInitTimer.stop();
				myInitTimer.removeEventListener(TimerEvent.TIMER, fadeIn);
				myInitTimer = null;
			}
		}

		private function onEnterFrame(e : Event) : void {
			myVitX += (myTargetXvit - myVitX) / 25;
			myVitY += (myTargetYvit - myVitY) / 25;

			x += myVitX;
			y += myVitY;

			if (x + width / 2 > myXlimit) {
				x = myXlimit - width / 2;
				myVitX *= -1;
				myTargetXvit *= -1;
			} else if (x - width / 2 < 0) {
				x = width / 2;
				myVitX *= -1;
				myTargetXvit *= -1;
			}
			if (y + height / 2 > myYlimit) {
				y = myYlimit - height / 2;
				myVitY *= -1;
				myTargetYvit *= -1;
			} else if (y - height / 2 < 0) {
				y = height / 2;
				myVitY *= -1;
				myTargetYvit *= -1;
			}

			var angle : Number = Math.atan(myVitY / myVitX) / (Math.PI / 180);
			if (myVitX < 0)
				angle += 180;
			if (myVitX >= 0 && myVitY < 0)
				angle += 360;

			rotation = angle - 90;
		}

		private function onDirectionTimer(e : TimerEvent) : void {
			myDirectionTimer.delay = Math.random() * 1500 + 500;
			myTargetXvit = Math.random() * 4 - 2;
			myTargetXvit = Math.random() * 4 - 2;
		}

		private function loop_lv5(e : Event) : void {
			x += target.x / 50;
			y -= 2;
		}

		public function destroy() : void {
			myDirectionTimer.stop();
			myDirectionTimer.removeEventListener(TimerEvent.TIMER, onDirectionTimer);
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		/*private function loop(pEvt : Event) : void {
		// Gestion de l'animation du vaisseau ennemi
		switch(_state) {
		case VIVANT:
		vy += ay;
		y += vy;
		// Si le vaisseau sort de l'Ã©cran, le supprimer
		if (y > stageRef.stageHeight) supprimerVaisseau();
		// Comportement du vaisseau
		}
		// if (y - 15 < target.y && y + 15 > target.y) tirerLaser();
		break;
		case DETRUIT:
		loadSWF("C:/workspace/tutoriel_shooter2012/src/com/telaxo/shooter2012/gfx/swf/StingerDetruit.swf");
		_state = DESTRUCTION_COMPLETE;
		break;
		case DESTRUCTION_COMPLETE:
		trace("CURRENT FRAME : " + image.currentFrame);
		trace("TOTAL FRAME : " + image.totalFrames);
		if (image.currentFrame == image.totalFrames) supprimerVaisseau();
		break;
		}
		}
		 * */
		public function estTouche() : void {
			_state = DETRUIT;
			rotation = Math.random() * 360;
		}

		public function tirerLaser() : void {
			stageRef.addChild(new BalleEnnemi(stageRef, target, x, y, -5));
			stageRef.addChild(new BalleEnnemi(stageRef, target, x, y, 5));
		}

		/*		public function supprimerVaisseau() : void {
		removeEventListener(Event.ENTER_FRAME, loop);

		if (stageRef.contains(this)) stageRef.removeChild(this);
		}
		 */
		public function set_state(val : Number) : void {
			_state = val;
		}

		// ACCESSEURS
		public function get_hit() : Sprite {
			return hitBox;
		}

		// Charge le swf correspondant au vaisseau ennemi
		private function loadSWF(swfURL : String) : void {
			var myLoader : Loader = new Loader();
			var mySWF : URLRequest = new URLRequest(swfURL);
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			myLoader.load(mySWF);
		}

		private function onCompleteHandler(loadEvent : Event) : void {
			if (this.contains(image)) removeChild(image);
			image = loadEvent.currentTarget.content;
			addChild(image);
			image.play();
		}

		private function onProgressHandler(myProgress : ProgressEvent) : void {
			var percent : Number = Math.round(myProgress.bytesLoaded / myProgress.bytesTotal * 100);
		}

		public function supprimerVaisseau() : void {
		}
	}
}
